package com.sc.web.gwt.server.service;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.sc.web.gwt.client.service.GetTokenKeyService;
import com.sc.web.gwt.server.hotpgenerator.OneTimePassword;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * Created by KienCT
 * Date: 29-05-2011
 * Time: 18:47:26
 */
public class GetTokenKeyServiceImpl extends RemoteServiceServlet implements GetTokenKeyService {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().write(getTokenKey());
    }

    public String getTokenKey() {
        String secretKey = "0979891120";
        long movingFactor = 100;
        try {
            return OneTimePassword.generateOTP(secretKey.getBytes(), movingFactor, 6, false, 5);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }
        return "Unexpected Error";
    }
}

